package my.maven.project.spiderproc.controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import my.maven.project.spiderproc.entity.Word;
import my.maven.project.spiderproc.service.IWordService;
import my.maven.project.spiderproc.spiders.CibaSpider;

@RestController
@RequestMapping("api")
public class ApiController {

	@Autowired
	private IWordService wordService;
	
	 @GetMapping("/demo")
	 public String demo()
	 {
		 String path = ApiController.class.getClassLoader().getResource("static/words.txt").getPath();
		 System.out.println(path);
		 return "Demo.String";
	 }
	 
	 @GetMapping("/word")
	 public List<Word> word()
	 {
		 return wordService.getList();
	 }
	 @GetMapping("/exists")
	 public boolean existsByWords(@RequestParam String word)
	 {
		return  wordService.existsByWords(word);
	 }
	 
	 @GetMapping("/like")
	 public List<Word> findByWordsLike(@RequestParam String word)
	 {
		return  wordService.findByWordsLike("%"+word+"%");
	 }
	 
	@GetMapping("/find")
	public Word find(@RequestParam String word) {
		Word exist = wordService.findByWords(word);
		try {
			if (exist == null) {
				Map<String, String> data = CibaSpider.ReuqestHttp(word);
				if(data != null ) {
					Word entity = new Word();
					entity.setCreateDate(new Date());
					entity.setMeans(data.get("means"));
					entity.setPronunce(data.get("pronunce"));
					entity.setWordChange(data.get("changes"));
					entity.setWords(data.get("words"));
					exist = entity;
					wordService.add(entity);
				}
			}
		} catch (IOException e) {
			return null;
		}
		System.out.println(exist.toString());
		return exist;
	}
	 
	@GetMapping("/getwords")
	public String getwords() throws Exception {
		try {
			String path = ApiController.class.getClassLoader().getResource("static/words.txt").getPath();
			File file = new File(path);
			FileReader st = new FileReader(file);
			BufferedReader br = new BufferedReader(st);
			
			Set<String> wordlist = new HashSet<>();
			String str;
			while((str = br.readLine()) != null)
			{
				wordlist.add(str.trim());
			}
			st.close();
			br.close();
			wordService.SpiderList(wordlist);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "request error";
		}
		return "success";
	}
	
	@GetMapping("/wordfile")
	public Set<String> getfile() throws Exception
	{
//		String path = ApiController.class.getClassLoader().getResource("static/words.txt").getPath();
//		File file = new File(path);
//		FileInputStream st = new FileInputStream(file);
//		byte[] buffer = new byte[(int) file.length()];
//		st.read(buffer);
//		st.close();
//		String words = new String(buffer);
//		String[] allword = words.split("\n");
		
		String path = ApiController.class.getClassLoader().getResource("static/words.txt").getPath();
		File file = new File(path);
		FileReader st = new FileReader(file);
		BufferedReader br = new BufferedReader(st);
		
		Set<String> wordlist = new HashSet<>();
		String str;
		while((str = br.readLine()) != null)
		{
			wordlist.add(str.trim());
		}
		st.close();
		br.close();
		return wordlist;
	}
	
	@GetMapping("/wordsin")
	public List<Word> getwordsin() throws Exception
	{
		 Set<String> words =  getfile();
		 words.removeIf(c-> c.length() >=3);
		 return wordService.findByWordsIn(words);
	}
	
	@GetMapping("/wordfile2")
	public String wordfile() 
	{
	    return getwordfile();
	}
	
	
	
	private String getwordfile()
	{
		String result = "";
		byte[] buffer = new byte[1024];
		try {
			InputStream filestream =   ApiController.class.getClassLoader().getResourceAsStream("/static/words.txt");
			int length = filestream.read(buffer);
			result = new String(buffer,0,length);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	
}
